/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.schema.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import offset.nodes.Constants;
import offset.nodes.client.chooser.view.TypeChooserDialog;
import offset.nodes.client.dialog.newnode.model.NameProvider;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.SimpleNamespaceRegistry;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeReader;
import offset.nodes.client.virtual.model.GetSchemas;
import offset.nodes.client.model.ActivePanelContainer;
import offset.nodes.client.model.Initializable;
import offset.nodes.client.model.Validator;

/**
 *
 * @author  Walter Lütgenau
 */
public class SelectNodeTypePanel extends javax.swing.JPanel implements Validator, NameProvider, Initializable {

    TypeChooserDialog newTypeChooser = null;
    SimpleNamespaceRegistry namespaces = SimpleNamespaceRegistry.getInstance();
    NodeTypes types = null;
    Server model = null;
    
    /** Creates new form SelectNodeTypePanel */
    public SelectNodeTypePanel(Server model) {
        initComponents();
        this.model = model;
    }

    public void init(ActivePanelContainer container) {
        initTypes(model);
    }

    protected void initTypes(Server server) {
        GetSchemas.Request request = new GetSchemas.Request();
        GetSchemas.Response response = null;
        try {
            response = (GetSchemas.Response) server.sendRequest(request);
            InputStream in = new ByteArrayInputStream(response.getSchemaXml().getBytes());
            NodeTypeDefinition[] definitions = NodeTypeReader.read(in);
            types = new NodeTypes(definitions);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean isValidPane() {
        if (nodeTypeTextField.getText() != null && nodeTypeTextField.getText().length() > 0) {
            return true;
        }
        return false;
    }

    public String getObjectName() {
        return nodeTypeTextField.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nodeTypeLabel = new javax.swing.JLabel();
        nodeTypeTextField = new javax.swing.JTextField();
        nodeTypeSelectButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle"); // NOI18N
        nodeTypeLabel.setText(bundle.getString("templateFolder.step1.nodeTypeLabel")); // NOI18N

        nodeTypeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nodeTypeTextFieldActionPerformed(evt);
            }
        });

        nodeTypeSelectButton.setText(bundle.getString("templateFolder.step1.selectNodeTypeButton")); // NOI18N
        nodeTypeSelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectNodeType(evt);
            }
        });

        jScrollPane1.setBorder(null);

        jTextPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        jTextPane1.setBorder(null);
        jTextPane1.setText(bundle.getString("templateFolder.step1.description")); // NOI18N
        jScrollPane1.setViewportView(jTextPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(nodeTypeLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nodeTypeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nodeTypeSelectButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nodeTypeLabel)
                    .addComponent(nodeTypeSelectButton)
                    .addComponent(nodeTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void nodeTypeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nodeTypeTextFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_nodeTypeTextFieldActionPerformed

    private void selectNodeType(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectNodeType
        if (newTypeChooser == null) 
            newTypeChooser = new TypeChooserDialog(null, true, 
                    types.getArray(new QName[] {new QName(Constants.URI_NODES_DATA, ""), new QName(Constants.URI_NODES, "")}));        
        
        newTypeChooser.setVisible(true);
        if (newTypeChooser.getReturnStatus() == TypeChooserDialog.RET_OK) 
            nodeTypeTextField.setText(namespaces.toPrefixName(newTypeChooser.getDefinition().getName()));
    }//GEN-LAST:event_selectNodeType
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JLabel nodeTypeLabel;
    private javax.swing.JButton nodeTypeSelectButton;
    private javax.swing.JTextField nodeTypeTextField;
    // End of variables declaration//GEN-END:variables
    
}
